
<template>
  <ArchiveItem ref="archive">
    <template #footer>
      <button @click="handleSubmit" class="d-btn d-btn-primary" type="button">提交</button>
      <a v-d-goback class="d-btn d-btn-default">取消</a>
    </template>
  </ArchiveItem>
</template>

<script>
import { zh as locale } from '@daelui/dogjs/dist/locale.js'
import { Dialoger } from '@daelui/vdog/dist/components.js'
import { objecter, parser } from '@daelui/dogjs/dist/components.js'
import arcSer from '@/service/action/archive.action'
import ArchiveItem from '../item.vue'

export default {
  components: {
    ArchiveItem
  },

  methods: {
    /**
     * @function 初始化视图
    */
    initView () {
      let params = this.$route.params
      this.chapterId = params.chapterId
    },

    handleSubmit () {
      let defer = null
      let view = this.$refs.archive
      let state = objecter.clone(view.state)
      delete view.state.createTime
      state.items = parser.stringify(state.items)
      // 目录文档
      state.cat = 3
      state.chapterId = this.chapterId
      // 编辑
      if (view.isEdit) {
        defer = arcSer.editItem(state)
      } else { // 添加
        defer = arcSer.addItem(state)
      }
      defer.then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
          // 添加则回到列表页
          this.$router.back()
        } else {
          Dialoger.danger(locale.action.fail)
        }
      })
    }
  },

  mounted () {
    this.initView()
  }
}

</script>